const Product = require('../model/product/product')
const Model = require('../model/product/productlist')
const Info = require('../model/product/ording')
const mongoose = require('mongoose')
const { Fproduct, Sproduct, Tproduct, State, Dynamic } = Model
const { Iproduct, Ording } = Info
const Pic = require('../model/product/pic')
const productController = {
    //获取商品列表
  async getProductList(req, res) {
    let body = req.query
    let pagenum = parseInt(body.pagenum) ? parseInt(body.pagenum) :  1//当前第几页  
    let pagesize = parseInt(body.pagesize) //每页个数
    let sort = {'id':-1}            //字段排序
    let skipnum = (pagenum - 1) * pagesize  //跳过页数
    // try {
    //    let total = await Product.countDocuments({})
    //    await Product.find({}).skip(skipnum).limit(pagesize).sort(sort).exec((err,doc)=>{
    //      return  res.status(200).json({
    //                status: 200,
    //                list: doc,
    //                total:total,
    //                msg:'获取商品列表信息成功'
    //              })            
    //    })
    // } catch (error) {
    //     res.status(500).json({
    //         error_code: 1,
    //         msg: '获取商品列表信息失败'
    //     })
    // }
    try {
      let total = await Fproduct.countDocuments({})
      await Fproduct.find({}).skip(skipnum).limit(pagesize).sort(sort).populate({
        path: 'children',
        select: '_id label pid level deleted',
        model: 'Sproduct',
        populate: {
            path: 'children',
            select: '_id label pid level deleted',
            model: 'Tproduct'
        }
      }).exec((err,doc)=>{
         return  res.status(200).json({
                   status: 200,
                   list: doc,
                   total:total,
                   msg:'获取商品列表信息成功'
                 })  
      })
    } catch (error) {
        res.status(500).json({
        error_code: 1,
        msg: '获取商品列表信息失败'
      })
    }
  },
  async createMulit(req,res) {
    const body = req.query
    let Tproduct1 = new Tproduct({
    _id: new mongoose.Types.ObjectId(),
    label: body.tlabel
  })
  try {
    await Tproduct.findOne({}).exec((err,doc)=>{
         Tproduct1.save()
         let Sproduct1 = new Sproduct({
            _id: new mongoose.Types.ObjectId(),
            label: body.slabel,
            children:Tproduct1._id
          })
          Sproduct1.save()
          let Fproduct1 = new Fproduct({
            _id: new mongoose.Types.ObjectId(),
            label: body.flabel,
            children:Sproduct1._id
          })
          Fproduct1.save()
          let State1 = new State({
            _id: new mongoose.Types.ObjectId(),
            iteam: body.iteam,
            status: body.status,
            system: body.system,
            apparent: body.apparent
          })
          State1.save()
          let Dynamic1 = new Dynamic({
            _id: new mongoose.Types.ObjectId(),
            class: body.class,
            desc: body.desc
          })
          Dynamic1.save()
          res.status(200).json(doc)
    })
  } catch (error) {
   res.status(500).json({
     error_code: 1,
     message: '服务器错误'
   })
  }
  },
  async createSpecial(req,res) {
    const body = req.query
    const id = body.id
    // const states = body.states
    const dynamics = body.dynamics
    try {
        await Tproduct.updateOne({'_id':id},{$push:{dynamics:dynamics}}).exec((err,doc)=>{
                res.status(200).json(doc)
            })

      } catch (error) {
       res.status(500).json({
         error_code: 1,
         message: '服务器错误'
       })
      }
  },
  async showProductState(req,res) {
    const id = req.params.id
    const path = req.query.sel
    const qath = path.toLowerCase()
    console.log(path)
    const choose = path == 'Dynamics'  ?  '_id class desc' : '_id iteam status system apparent'
    console.log(choose)
    console.log(req)
    try {
      await Tproduct.findOne({_id:id}).populate({
        path: qath,
        select: choose,
        model: this.path
      })
      .exec((err,doc)=>{
        return res.status(200).json({
          msg:'获取商品列表信息成功',
          doc:doc
        })
      })
      
    } catch (error) {
      res.status(500).json({
        error_code: 1,
        message: '服务器错误'
      })
    }
  },
  async addDynamic(req,res) {
    // const id = req.params.id
    const StateId = req.params.StateId
    const body = req.body.params
    console.log(req)
    // console.log(id)
    console.log(body)
    try { 
        await Dynamic.updateOne({_id:StateId},{...body}).exec((err,doc)=>{   
          res.status(200).json(doc)
        })
    } catch (error) {
      res.status(500).json({
        error_code: 1,
        message: '服务器错误'
      })
    }
  },
  async getProInfo(req,res) {
    const body = req.query
    let Ording1 = new Ording({
    _id: new mongoose.Types.ObjectId(),
    ordId: body.ordId,
    price: body.price
  })
  try {
    await Ording.findOne({}).exec((err,doc)=>{
      Ording1.save()
         let Iproduct1 = new Iproduct({
            _id: new mongoose.Types.ObjectId(),                                                                
            label: body.tlabel,
            price: Ording1.price,
            num: body.num,
            ords:Ording1._id
          })
          Iproduct1.save()
          res.status(200).json(doc)
    })
  } catch (error) {
   res.status(500).json({
     error_code: 1,
     message: '服务器错误'
   })
  }
  },
  async getProList(req,res) {
    try {
      Iproduct.find({}).exec((err,doc)=>{
        res.status(200).json(doc)
      })
    } catch (error) {
      res.status(500).json({
        error_code: 1,
        message: '服务器错误'
      })
    }
  },
  async getPic(req,res) {
    const body = req.query
    let Pic1 = new Pic({
      path: body.path
    })
    try {
      await Pic.findOne({}).exec((err,doc)=>{
        Pic1.save()
        res.status(200).json(doc)
      })
    } catch (error) {
      
    }
  },
  async addNewPro(req,res) {
    const body = req.body.params
    console.log(req)
    let Iproduct1 = new Iproduct({
    _id: new mongoose.Types.ObjectId(),
    label: body.name,
    price: body.price,
    num: body.num
  })
  try {
    await Iproduct.findOne({}).exec((err,doc)=>{
          Iproduct1.save()
          res.status(200).json(doc)
      })
    } catch (error) {
      res.status(500).json({
        error_code: 1,
        message: '服务器错误'
      })
    }
  },
  async deletePro(req,res) {
    let _id = req.params._id
    console.log(_id)
    try {
      await Iproduct.findOne({_id:_id}).remove().exec((err,result) =>{
        if(result){
          return res.status(200).json({
            message: '删除成功！'
          })
        }
        res.status(201).json({
          message: '删除失败！'
        })
      })     
    } catch (error) {
        res.status(500).json({
            error_code: 1,
            message: '服务器错误'
      })
    }
  },
  async getOrding(req,res) {
    try {
      await Ording.find({}).exec((err,doc)=> {
        res.status(200).json(doc)
      })
    } catch (error) {
      res.status(500).json({
        error_code: 1,
        message: '服务器错误'
      })
    }
  }
}
module.exports = productController